<?php
/*
 * @Author: 李志刚
 * @CopyRight: 2020-2030 衡水山木枝技术服务有限公司
 * @Date: 2021-06-10 20:18:18
 * @Description: 供应商管理
 * @LastEditors: 李志刚
 * @LastEditTime: 2021-09-17 08:37:36
 * @FilePath: /qingshanERP/app/Http/Controllers/Console/QingShanERP/SupplierController.php
 */

namespace App\Http\Controllers\Console\QingShanERP;

use App\Http\Controllers\Controller;
use App\Models\QingShanERP\Supplier;
use App\Models\Tenant\SupplierTenant;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Validator;

class SupplierController extends Controller
{
    public function getList(Request $req)
    {
        try {
            $page = $req->input('page', 0);
            $size = $req->input('size', 10);
            $key = $req->input('key', '');
            $status = $req->input('status', '');
            $list = Supplier::where(function ($q) use ($key) {
                if ($key != '') {
                    $q->where('suppliername', 'like', '%' . $key . '%');
                }
            })->where(function ($q) use ($status) {
                if ($status != '') {
                    $q->where('status', $status);
                }
            })->where('del_flag', 0)->offset(($page - 1) * $size)->limit($size)->orderBy('id', 'desc')->get();
            $count = Supplier::where(function ($q) use ($key) {
                if ($key != '') {
                    $q->where('suppliername', 'like', '%' . $key . '%');
                }
            })->where(function ($q) use ($status) {
                if ($status != '') {
                    $q->where('status', $status);
                }
            })->where('del_flag', 0)->count();
            $res = ['list' => $list, 'total' => $count];
            return $this->resData(200, '获取数据成功...', $res);
        } catch (\Throwable $e) {
            return $this->resData(500, '获取数据失败，请稍后再试...');
        }
    }

    public function postCreate(Request $request)
    {
        DB::beginTransaction();
        try {
            $validator = Validator::make($request->input(), [
                'suppliername' => 'required|max:100',
            ]);
            $attrs = array(
                'suppliername' => '供应商名称',
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                DB::rollBack();
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $all = $request->all();
            $create = ['suppliername' => $all['suppliername'], 'refund_address_id'=>$all['refund_address_id'],'name' => $all['name'],'phone' => $all['phone'],'province' => $all['province'],'city' => $all['city'],'district' => $all['district'], 'detail' => $all['detail'], 'status' => 1];
            Supplier::create($create);
            DB::commit();
            return $this->resData(200, '添加成功...');
        } catch (\Throwable $e) {
            DB::rollBack();
            return $this->resData(500, '添加失败，请重新操作...',$e->getLine().' - '.$e->getMessage());
        }
    }
    //详情
    public function postDetail(Request $request)
    {
        try {
            $validator = Validator::make($request->input(), [
                'supplier_id' => 'required',
            ]);
            $attrs = array(
                'supplier_id' => '供应商ID',
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $id = $request->input('supplier_id');
            $info = Supplier::find($id);
            return $this->resData(200, '获取数据成功...', $info);
        } catch (\Throwable $e) {
            return $this->resData(500, '获取数据失败，请重新操作...');
        }
    }
    //编辑
    public function postEdit(Request $request)
    {
        DB::beginTransaction();
        try {
            $validator = Validator::make($request->input(), [
                'supplier_id' => 'required',
                'suppliername' => 'required|max:100',
                'status' => 'required|in:true,false',
            ]);
            $attrs = array(
                'supplier_id' => '供应商ID',
                'suppliername' => '供应商名称',
                'status' => '状态',
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                DB::rollBack();
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $id = $request->input('supplier_id');
            $all = $request->all();
            $update = ['suppliername' => $all['suppliername'],'refund_address_id' => $all['refund_address_id'], 'name' => $all['name'], 'phone' => $all['phone'], 'province' => $all['province'], 'city' => $all['city'], 'district' => $all['district'], 'detail' => $all['detail'], 'status' => $all['status'] == 'true' ? 1 : 0];
            Supplier::where('id', $id)->update($update);
            DB::commit();
            return $this->resData(200, '编辑成功...');
        } catch (\Throwable $e) {
            DB::rollBack();
            return $this->resData(500, '编辑失败，请重新操作...',$e->getMessage());
        }
    }

    //删除
    public function postRemove(Request $request)
    {
        try {
            $validator = Validator::make($request->input(), [
                'supplier_id' => 'required|array',
            ]);
            $attrs = array(
                'supplier_id' => '供应商ID',
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $id = $request->input('supplier_id');
            Supplier::whereIn('id', $id)->update(['del_flag' => 1]);
            return $this->resData(200, '删除成功...');
        } catch (\Throwable $e) {
            return $this->resData(500, '删除失败，请重新操作...');
        }
    }
}
